<template>
	<view style="background-color: #f1f1f1;height: 100vh;">
		<view class="two">
			<u--form :model="form" ref="uForm" :rules="rules">
				<view class="two-one1">
					<view class="two-oneLeft1">地点:</view>
					<view class="two-oneRight1">{{dataForm.YFCY}}</view>
				</view>
				<view class="two-one1">
					<view class="two-oneLeft1">申请单号:</view>
					<view class="two-oneRight1">{{dataForm.YNUM}}</view>
				</view>
				<view class="two-one1">
					<view class="two-oneLeft1">申请日期 :</view>
					<view class="two-oneRight1">{{dataForm.YDAT}}</view>
				</view>
				<view class="two-one1">
					<view class="two-oneLeft1">设备编号 :</view>
					<view class="two-oneRight1">{{form.ysbitm}}</view>
				</view>
				<view class="two-one1">
					<view class="two-oneLeft1">申请人 :</view>
					<view class="two-oneRight1">{{dataForm.YSQDNAM}}</view>
				</view>
				<view class="two-one">
					<u-form-item style="padding-left: 20rpx;" label="检验员 :" labelWidth="150" prop="YJYYNAM">
					</u-form-item>
				 </view>
				 <view style="width: 80%;margin: 0 auto;margin-top: -70rpx;margin-left: 130rpx;">
				 	<uni-combox v-model="dataForm.YJYYNAM" :candidates="jyylist" @input="inputjyylist"
				 		placeholder="请选择" emptyTips="无数据" 
				 	></uni-combox>
				 </view>
				<view class="two-one">
					<u-form-item style="padding-left: 20rpx;" label="检验类型 :" labelWidth="150" prop="YTYPE">
					</u-form-item>
				</view>
				<view style="width: 73%;margin: 0 auto;margin-top: -70rpx;margin-left: 170rpx;">
					<uni-data-select v-model="dataForm.YTYPE" :localdata="range13" @change="toggleYTYPE" :clear="false">
					</uni-data-select>
				</view>
				<view v-if="dataForm.YTYPE==2 || dataForm.YTYPE==3 || dataForm.YTYPE==4" key="YTYPE1">
					<view class="two-one">
						<u-form-item style="padding-left: 20rpx;" label="产品 :" labelWidth="150" prop="ITMREF">
						</u-form-item>
					</view>
					<view style="width: 80%;margin: 0 auto;margin-top: -70rpx;margin-left: 130rpx;">
						<uni-combox v-model="dataForm.ITMREF" :candidates="sblist" @input="inputYsbList"
							placeholder="请选择" emptyTips="无数据" @change="changeYsbRang"
						></uni-combox>
					</view>
					<view class="two-one" style="width: 95%;margin: 0 auto;margin-top: 20rpx;">
						<u-form-item label="工艺工序 :" labelWidth="75" prop="YGYGX">
						</u-form-item>
						<u-input placeholder="请输入" v-model="dataForm.YGYGX" @change="change($event,'YGYGX')"/>
					</view>
				</view>
				<view v-if="dataForm.YTYPE==2 || dataForm.YTYPE==4" key="YTYPE2">
					<view class="two-one" style="width: 95%;margin: 0 auto;margin-top: 20rpx;">
						<u-form-item label="件号 :" labelWidth="75" prop="YJH">
						</u-form-item>
						<u-input placeholder="请输入" v-model="dataForm.YJH" @change="change($event,'YJH')"/>
					</view>
					<view class="two-one">
						<u-form-item style="padding-left: 20rpx;" label="部位 :" labelWidth="150" prop="YJCWZ">
						</u-form-item>
					</view>
					<view style="width: 73%;margin: 0 auto;margin-top: -70rpx;margin-left: 170rpx;">
						<uni-data-select :disabled="false" v-model="dataForm.YJCWZ" :localdata="range3" @change="change($event,'YJCWZ')" :clear="false">
						</uni-data-select>
					</view>
				</view>
				<view v-if="dataForm.YTYPE==4" key="YTYPE3">
					<view class="two-one">
						<u-form-item style="padding-left: 20rpx;" label="检测时机 :" labelWidth="150" prop="YDETTIM">
						</u-form-item>
					</view>
					<view style="width: 73%;margin: 0 auto;margin-top: -70rpx;margin-left: 170rpx;">
						<uni-data-select :disabled="false" v-model="dataForm.YDETTIM" :localdata="range1" @change="change($event,'YDETTIM')" :clear="false">
						</uni-data-select>
					</view>
					<view class="two-one">
						<u-form-item label="" prop="checkboxValue1" borderBottom labelWidth="80" ref="item3">
							<u-checkbox-group v-model="dataForm.checkboxValue1" shape="square" >
								<u-checkbox
								:customStyle="{marginRight: '16px'}"
								v-for="(item, index) in checkboxList1"
								:key="index"
								:label="item.name"
								:name="item.name"
								>
								</u-checkbox>
							</u-checkbox-group>
						</u-form-item>
					</view>
				</view>
			</u--form>
		</view>
		<div >
			<view class="four-item">
				<u-button class="rgzxBox-btn2" type="primary" v-show="this.syqrFlg"
				@click="submit()" >提交</u-button>
			</view>
		</div>
	</view>
</template>

<script>
	import { format } from 'date-fns';
	import * as dd from 'dingtalk-jsapi'

	export default {
		data() {
			return {
				syqrFlg:true,
				dataForm:{
					YFCY:'KX',
					YDAT:format(new Date(), 'yyyy-MM-dd'),
					YSQDNAM:',',
					YSQDID:'',
					checkboxValue1: [],
					YNUM:'',
					YTYPE:1,
					YGYGX:''
				},
				form: {
					ysbitm:'',
					YTYPE:''
				},
				loading: false,
				rules: {
					'YJCWZ': {
						type: 'string',
						required: true,
						message: '部位必填',
						trigger: ['blur', 'change']
					},
					'YJH': {
						type: 'string',
						required: true,
						message: '件号必填',
						trigger: ['blur', 'change']
					},
					'YJYYNAM': {
						type: 'string',
						required: true,
						message: '检验员必填',
						trigger: ['blur', 'change']
					},
					'ITMREF': {
						type: 'string',
						required: true,
						message: '产品必填',
						trigger: ['blur', 'change']
					},
					'YGYGX': {
						type: 'string',
						required: true,
						message: '工艺工序必填',
						trigger: ['blur', 'change']
					},
					'YDETTIM': {
						type: 'string',
						required: true,
						message: '检测时机必填',
						trigger: ['blur', 'change']
					},
				},
				test:false,
				jyylist:[],
				range13: [{value:1,text:"篮球"}],
				range3: [],
				range1: [],
				sblist: [],
				astList1058:[],
				checkboxList1: [{
					name: '含内件焊缝',
					disabled: false,
					},
				]
			}
		},
		onLoad(query) {
			this.userInfo = uni.getStorageSync('userInfo');
			this.init();
			this.test = uni.getStorageSync('test')||false;
			this.getjyy();
			this.getysb();
			this.AST6131();
			this.AST1092();
			this.AST1058();
			if (query.num){
				this.dataForm.YNUM=query.num
				this.getData(this.dataForm.YNUM)
			}
		},
		mounted() {

		  },
		methods: {
			init(){
				this.dataForm.YSQDNAM=this.userInfo.NAME
				this.dataForm.YSQDID=this.userInfo.ID
			},
			async getData(val){
				let GRP12 = {
					GRP1: [{
						YNUM: val,
					}]
				}
				let res2 = await this.$api.x3.x3request("WSLISTYPCT",GRP12)
				let wpdData2 = JSON.parse(res2.message)
				 this.dataForm=wpdData2.GRP1[0]

				this.form.ysbitm=this.dataForm.ITMREF
				this.dataForm.ITMREF=this.dataForm.ITMDES
				if (this.dataForm.YHNJ==2){
					this.dataForm.checkboxValue1=['含内件焊缝']
				}
				this.syqrFlg=false
			},
			toggleYTYPE() {
			      this.$nextTick(() => {
			        this.$refs.uForm.clearValidate('YTYPE1'); // 清除旧校验状态
			      });
				  this.$nextTick(() => {
				    this.$refs.uForm.clearValidate('YTYPE2'); // 清除旧校验状态
				  });
				  this.$nextTick(() => {
				    this.$refs.uForm.clearValidate('YTYPE3'); // 清除旧校验状态
				  });
			    },
			change(e,yfile) {
				if(yfile=='YGYGX'){
					this.form.YGYGX = e
				}
				if(yfile=='YJH'){
					this.form.YJH = e
				}
				if(yfile=='YJCWZ'){
					this.form.YJCWZ = e
				}
				if(yfile=='YDETTIM'){
					this.form.YDETTIM = e
				}
			},
			inputjyylist(e){
				this.form.YJYYNAM=e
			},
			inputYsbList(e){
				const extractedContent = this.extractBracketContent(e);
				this.form.ysbitm=extractedContent;
				this.changeYsbRang(this.form.ysbitm);
			},
			extractBracketContent(str) {
			  const regex = /【([^】]*)】/g;
			  const matches = str.match(regex);
			
			  if (matches) {
			    // 如果存在匹配项，我们只关心第一个（如果有多个）
			    // 并且去掉两边的中括号
			    const firstMatch = matches[0];
			    return firstMatch.replace(/【|】/g, '');
			  }
			  return null; // 如果没有找到匹配项，返回 null
			},
			async AST6131() {
				let GRP1 = {
					GRP1: [{
						LANCHP: 6131
					}]
				}
				let res = await this.$api.x3.x3request("WSLISTAST",GRP1)
				// 请求成功的处理逻辑
				let resR1 = JSON.parse(res.message)
				this.astList1058= resR1.GRP1
				this.range13=[]
				var newObject={}
				for (var i = 0; i < this.astList1058.length; i++) {
					newObject = { value: this.astList1058[i].LANNUM, text: this.astList1058[i].LANMES};
					this.range13.push(newObject)
				}
			},
			async AST1058() {
				let GRP1 = {
					GRP1: [{
						LANCHP: 1058
					}]
				}
				let res = await this.$api.x3.x3request("WSLISTAST",GRP1)
				// 请求成功的处理逻辑
				let resR1 = JSON.parse(res.message)
				this.astList1058= resR1.GRP1
				this.range1=[]
				var newObject={}
				for (var i = 0; i < this.astList1058.length; i++) {
					newObject = { value: this.astList1058[i].LANNUM, text: this.astList1058[i].LANMES};
					this.range1.push(newObject)
				}
			},
			async AST1092() {
				let GRP1 = {
					GRP1: [{
						LANCHP: 1092
					}]
				}
				let res = await this.$api.x3.x3request("WSLISTAST",GRP1)
				// 请求成功的处理逻辑
				let resR1 = JSON.parse(res.message)
				this.astList1058= resR1.GRP1
				this.range3=[]
				var newObject={}
				for (var i = 0; i < this.astList1058.length; i++) {
					newObject = { value: this.astList1058[i].LANNUM, text: this.astList1058[i].LANMES};
					this.range3.push(newObject)
				}
			},
			async changeYsbRang(e) {
			  	this.form.ITMREF = e
			// 	this.dataList.YITMREF = e+'-';
			// 	let GRP1 = {GRP1:[{ITMREF:e}]}
			// 	if(e.length>5){
			// 		let res = await this.$api.x3.x3request("WSLISTITM",GRP1)
			
			// 			let cusbData = JSON.parse(res.message)
			// 			this.dataList.YITMTH = cusbData.GRP1.YITMTH;
			// 			this.dataList.YITMWH = cusbData.GRP1.YITMWH;
			// 	}
			 },
			async getysb(){
				let GRP1 = {GRP1:[{TCLCOD:'FGMSN'}]}
				let res = await this.$api.x3.x3request("WSLISTYSB",GRP1)
			
					let cusbData = JSON.parse(res.message)
					this.ysbRang = cusbData.GRP1.map(item => {
					      return { value: item.ITMREF, text: item.ITMDES1 };
					    });
					this.sblist=[];
					for (var i = 0; i < cusbData.GRP1.length; i++) {
						this.sblist.push('【'+cusbData.GRP1[i].ITMREF+'】'+cusbData.GRP1[i].ITMDES1)
					}
			},
			async getjyy(){
				let GRP1 = {}
				let res = await this.$api.x3.x3request("WSLISTYDR2",GRP1)
				let cusbData = JSON.parse(res.message)
				this.jyylist=[];
				for (var i = 0; i < cusbData.GRP1.length; i++) {
					this.jyylist.push(cusbData.GRP1[i].NAME)
				}
			},
			
			submit() {
				if (undefined==this.dataForm.YTYPE || this.dataForm.YTYPE=='' || this.dataForm.YTYPE=='1'){
					uni.$u.toast('检验类型必填')
					return
				}
				
				this.$refs.uForm.validate().then(res => {
					var this_ = this
					uni.showModal({
					  title: '提示',
					  content: '是否提交？',
					  success: function (res) {
					    if (res.confirm) {
					      this_.submitData();
					    }
					  }
					});
				}).catch(errors => {
					uni.$u.toast('校验失败')
				})
			},
			async submitData() {
				let GRP1 = {
					GRP1: [{
							YSQDID:this.dataForm.YSQDID,
							YSQDNAM:this.dataForm.YSQDNAM,
							YJYYNAM:this.dataForm.YJYYNAM,
							YFCY:this.dataForm.YFCY,
							YTYPE:this.dataForm.YTYPE,
							ITMREF:'',
							YGYGX:'',
							YJH:'',
							YJCWZ:0,
							YDETTIM:0,
							YHNJ:1,
					}]
				}

				if(this.dataForm.YTYPE==2 || this.dataForm.YTYPE==3 || this.dataForm.YTYPE==4){
					GRP1.GRP1[0].ITMREF=this.form.ysbitm
					GRP1.GRP1[0].YGYGX=this.dataForm.YGYGX
				}
				if(this.dataForm.YTYPE==2 || this.dataForm.YTYPE==4){
					GRP1.GRP1[0].YJH=this.dataForm.YJH
					GRP1.GRP1[0].YJCWZ=this.dataForm.YJCWZ
				}
				if(this.dataForm.YTYPE==4){
					GRP1.GRP1.YDETTIM=this.dataForm.YDETTIM
					if(this.dataForm.checkboxValue1.length>0){
						GRP1.GRP1[0].YHNJ=2
					}
				}

				let res = await this.$api.x3.x3request("WSGENYPCT",GRP1)
				let resR = JSON.parse(res.message)
				if(resR.GRP1.WOK==0){
					// 显示创建成功的消息提示
					uni.showToast({
					   title: '提交成功:'+resR.GRP1.WRESULT,
					   icon: 'success',
					   duration: 2000
					});
				let that = this
					this.form =''
					this.dataForm=''
					this.tableData=[]
					this.tableDataAll=[]
					uni.switchTab({url:"/pages/managementCenter/managementCenter"})
				}else{
					uni.showToast({
					   title: '提交失败',
					   icon: 'error',
					   duration: 2000
					});
				}
			},
		}
	}
</script>

<style>
.top{
	height: 100rpx;
	line-height: 100rpx;
	color: #13d5f6;
}
.two{
	background-color: white;
}
.two-one{
	overflow: hidden;
	margin-top: 20rpx;
	display: flex;
	justify-content: space-between;
}
.two-oneLeft{
	width: 190rpx;
	color: gray;
	text-align: right;
}
.two-oneRight{
	width: 350rpx;
	color: gray;
	text-align: left;
	margin-left: 10rpx;
}
.two-one1{
	overflow: hidden;
	margin-top: 20rpx;
	display: flex;
}
.two-oneLeft1{
	width: 200rpx;
	color: gray;
	text-align: right;
}
.two-oneRight1{
	color: gray;
	text-align: left;
	margin-left: 10rpx;
}
.there{
	height: 100rpx;
	line-height: 100rpx;
	color: #13d5f6;
}
.four{
	height: 120rpx;
	background-color: white;
}
.four-one{
	height: 60rpx;
	display: flex;
	justify-content: space-between;
}
.four-oneLeft{
	width: 190rpx;
	line-height: 68rpx;
	color: gray;
	text-align: right;
}
.four-oneRight{
	width: 350rpx;
	line-height: 68rpx;
	color: gray;
	text-align: left;
}
.five{
	height: 100rpx;
	line-height: 100rpx;
	color: #13d5f6;
}
.sixBox{
	height: 70rpx;
	background-color: white;
	line-height: 37rpx;
	padding-left: 40rpx;
	margin-bottom: 20rpx;
}
.four-item{
	width: 90%;
	margin-top: 16rpx;
	margin-left: 40rpx;
}
</style>
